﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmChuyenKho : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        public static int daluu = 0;
        public static MaVachBO MaVachBO = MaVachBO.NewRootMaVachBO();
        private TonKhoBO tonKhoBON = TonKhoBO.NewRootTonKhoBO();
        private TonKhoBO tonKhoBOD = TonKhoBO.NewRootTonKhoBO();
        private MaVachBO MaVachBOD = MaVachBO.NewRootMaVachBO();
        private QuyDoiBOCollection quyDoiBOColl = QuyDoiBOCollection.NewQuyDoiBOCollection();
        private bool duocchuyen = false;

        private double soluongChuyen = 0;
        private double soluongQuyDoi = 0;
        private double soluongN = 0;
        private double soluongD = 0;
        #endregion variant

        //=====================================
        #region form
        public FrmChuyenKho()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmChuyenKho_Load);
            this.Shown += new EventHandler(FrmChuyenKho_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmChuyenKho_FormClosing);

            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.KeyDown += new KeyEventHandler(FrmChuyenKho_KeyDown);
        }

        void FrmChuyenKho_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else            
                if (this.btnSave.Visible && ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6)))
                    Save();
                
        }

        private void FrmChuyenKho_Shown(object sender, EventArgs e)
        {
            this.lookUpMatHang.Focus();
        }

        private void FrmChuyenKho_Load(object sender, EventArgs e)
        {
            Utility.InitControl(this.lookUpMatHang);
            Utility.InitControl(this.lookUpDonViTinhIDN);
            Utility.InitControl(this.lookUpKhoIDN);
            Utility.InitControl(this.lookUpMatHangIDD);
            Utility.InitControl(this.lookUpDonViTinhIDD);

            RefreshData();
        }

        private void FrmChuyenKho_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            FrmChuyenKho.daluu = 0;
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {

            MatHangBOList _matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.lookUpMatHang, _matHangBOList, _matHangBOList.Count);

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.lookUpDonViTinhIDN, donViTinhBOList, donViTinhBOList.Count);

            DanhMucBOList danhMucKhoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            Utility.SetData(this.lookUpKhoIDN, danhMucKhoBOList, danhMucKhoBOList.Count);

            if (MaVachBO != null)
            {
                txtMaSo.Text = MaVachBO.MaVach.Trim();
                tonKhoBON = TonKhoBO.GetRootTonKhoBO(StaticUtil.GetInt(MaVachBO.TonKhoID.Value, -1));
                lookUpMatHang.EditValue = StaticUtil.GetInt(tonKhoBON.MatHangID.Value, -1);
                lookUpDonViTinhIDN.EditValue = StaticUtil.GetInt(tonKhoBON.DonViTinhID.Value, -1);
                lookUpKhoIDN.EditValue = StaticUtil.GetInt(tonKhoBON.DanhMucKhoID.Value, -1);
                soluongN = StaticUtil.GetDouble(MaVachBO.SoLuongTon.Value, 0);
                spinSoLuongN.Value = (decimal)soluongN;


                quyDoiBOColl = QuyDoiBOCollection.GetQuyDoiBOCollection("MatHangID = " + StaticUtil.GetInt(tonKhoBON.MatHangID.Value, -1).ToString());

                MatHangBOList _MatHangBOList = matHangBOList(quyDoiBOColl);
                Utility.SetData(this.lookUpMatHangIDD, _MatHangBOList, _MatHangBOList.Count);

                DanhMucBOList donViTinhBOListD = donViTinhBOListDich(quyDoiBOColl);
                Utility.SetData(this.lookUpDonViTinhIDD, donViTinhBOListD, donViTinhBOListD.Count);
            }
            
        }

        private MatHangBOList matHangBOList(QuyDoiBOCollection quydoiTemp)
        {
            MatHangBOList temp = null;
            #region Lấy list các IDMatHang được quy đổi...he he
            
            string listID = "";
            int recordCount = quydoiTemp.Count;
            if (recordCount > 0)
            {
                for (int i = 0; i < recordCount; i++)
                {
                    object _idVattu = quydoiTemp[i].MatHang2ID;
                    listID += _idVattu.ToString() + ",";
                }
            }

            if (listID != "")
            {
                string strWhere = " MatHangID in ('" + listID.Replace(",", "','") + "')";
                temp =
                    MatHangBOList.GetMatHangBOList(strWhere);
            }
            #endregion

            return temp;
        }


        private DanhMucBOList donViTinhBOListDich(QuyDoiBOCollection quydoiTemp)
        {
            DanhMucBOList temp = null;
            #region Lấy list các IDMatHang được quy đổi...he he

            string listID = "";
            int recordCount = quydoiTemp.Count;
            if (recordCount > 0)
            {
                for (int i = 0; i < recordCount; i++)
                {
                    object _idVattu = quydoiTemp[i].DonViTinh2ID;
                    listID += _idVattu.ToString() + ",";
                }
            }

            if (listID != "")
            {
                string strWhere = " DanhMucID in ('" + listID.Replace(",", "','") + "')";
                temp =
                    DanhMucBOList.GetDanhMucBOList(strWhere);
            }
            #endregion

            return temp;
        }

        private void Save()
        {
            try
            {
                if (duocchuyen)
                {
                    tonKhoBON.SoLuongTon = tonKhoBON.SoLuongTon.Value - soluongChuyen;
                    tonKhoBON.Save();
                    MaVachBO.SoLuongTon = StaticUtil.GetDouble(spinSoLuongN.Value, soluongN);
                    MaVachBO.Save();

                    tonKhoBOD.SoLuongTon = StaticUtil.GetDouble(tonKhoBOD.SoLuongTon.Value, 0) + soluongQuyDoi;
                    tonKhoBOD.Save();

                    MaVachBOD.SoLuongTon = StaticUtil.GetDouble(MaVachBOD.SoLuongTon.Value, soluongD) + soluongQuyDoi;
                    MaVachBOD.TonKhoID = StaticUtil.GetInt(tonKhoBOD.TonKhoID.Value, -1);
                    MaVachBOD.Save();

                    ChuyenKhoBO chuyenKhoBO = ChuyenKhoBO.NewRootChuyenKhoBO();
                    chuyenKhoBO.MatHangID = StaticUtil.GetInt(tonKhoBON.MatHangID.Value, -1);
                    chuyenKhoBO.TonKhoIDN = StaticUtil.GetInt(tonKhoBON.TonKhoID.Value, -1);
                    chuyenKhoBO.DonViTinhIDN = StaticUtil.GetInt(tonKhoBON.DonViTinhID.Value, -1);
                    chuyenKhoBO.KhoIDN = StaticUtil.GetInt(tonKhoBON.DanhMucKhoID.Value, -1);
                    chuyenKhoBO.TonKhoIDD = StaticUtil.GetInt(tonKhoBOD.TonKhoID.Value, -1);
                    chuyenKhoBO.MatHangIDD = StaticUtil.GetInt(tonKhoBOD.MatHangID.Value, -1);
                    chuyenKhoBO.DonViTinhIDD = StaticUtil.GetInt(tonKhoBOD.DonViTinhID.Value, -1);
                    chuyenKhoBO.KhoIDD = StaticUtil.GetInt(tonKhoBON.DanhMucKhoID.Value, -1);
                    chuyenKhoBO.SoLuongChuyen = soluongChuyen;
                    chuyenKhoBO.SoLuongQuyDoi = soluongQuyDoi;
                    chuyenKhoBO.DienGiai = "QuyĐổi-Tách-Cắt "+ lookUpMatHang.Text.Trim() + " trong " + lookUpKhoIDN.Text.Trim();
                    chuyenKhoBO.Save();

                    FrmChuyenKho.daluu = 1;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Không có hệ số chuyển đổi này! ", "Cảnh báo! ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
            catch
            {

            }
        }
        #endregion function

        private void lookUpKhoD_EditValueChanged(object sender, EventArgs e)
        {
            int _DonViTinhIDD = StaticUtil.GetInt(lookUpDonViTinhIDD.EditValue, -1);
            int _MatHangIDD = StaticUtil.GetInt(lookUpMatHangIDD.EditValue, -1);

            QuyDoiBO quyDoiBO = quyDoiBOColl.GetQuyDoiBO(_MatHangIDD, _DonViTinhIDD);

            if (StaticUtil.GetInt(quyDoiBO.QuyDoiID.Value, -1) < 0)
            {
                duocchuyen = false;
                quyDoiBO = QuyDoiBO.NewRootQuyDoiBO();
                txtBangChu.Text = "Không có hệ số Quy đổi này";
            }
            else
            {
                duocchuyen = true;

                soluongChuyen = (double)spinSoLuongChuyen.Value;
                double slConLai = soluongN - soluongChuyen;
                if (slConLai >= 0)
                {
                    txtBangChu.Text = "1 " + lookUpDonViTinhIDN.Text.Trim() + " " + lookUpMatHang.Text.Trim() 
                        + " đổi được " + StaticUtil.GetDouble(quyDoiBO.HeSoQuyDoi.Value, 0)
                        + lookUpDonViTinhIDD.Text.Trim() + " " + lookUpMatHangIDD.Text.Trim();

                    tonKhoBOD = TonKhoBO.GetRootTonKhoBOByWhere("(MatHangID = " + _MatHangIDD.ToString()
                                + ") and (DonViTinhID = " + _DonViTinhIDD.ToString()
                                + ") and (DanhMucKhoID = " + StaticUtil.GetInt(tonKhoBON.DanhMucKhoID.Value, -1).ToString() + ")");

                    if (StaticUtil.GetInt(tonKhoBOD.TonKhoID.Value, -1) < 0)
                    {
                        tonKhoBOD = TonKhoBO.NewRootTonKhoBO();
                        tonKhoBOD.DonViTinhID = _DonViTinhIDD;
                        tonKhoBOD.MatHangID = _MatHangIDD;
                        tonKhoBOD.DanhMucKhoID = StaticUtil.GetInt(tonKhoBON.DanhMucKhoID.Value, -1);
                        tonKhoBOD.SoLuongTon = 0;

                        MaVachBOD = MaVachBO.NewRootMaVachBO();
                        MaVachBOD.MaVach = txtMaSo.Text;
                        MaVachBOD.SoLuongTon = 0;

                        soluongQuyDoi = 0;
                        soluongChuyen = StaticUtil.GetDouble(spinSoLuongChuyen.Value, 0);
                        soluongQuyDoi = soluongChuyen * StaticUtil.GetDouble(quyDoiBO.HeSoQuyDoi.Value, 0);
                        spinSoLuongQuyDoi.Value = (decimal)soluongQuyDoi;
                        soluongD = 0;
                        spinSoLuongN.Value = (decimal)soluongN - (decimal)soluongChuyen;
                        spinSoLuongD.Value = (decimal)soluongD + (decimal)soluongQuyDoi;
                    }
                    else
                    {
                        MaVachBOD = MaVachBO.GetRootMaVachBOByWhere("TonKhoID = " + tonKhoBOD.TonKhoID.ToString() + " and MaVach = " + txtMaSo.Text.Trim());

                        if (StaticUtil.GetInt(MaVachBOD.MaVachID.Value, -1) < 0)
                        {
                            MaVachBOD = MaVachBO.NewRootMaVachBO();
                            MaVachBOD.TonKhoID = tonKhoBOD.TonKhoID.Value;
                            MaVachBOD.MaVach = txtMaSo.Text;
                            MaVachBOD.SoLuongTon = 0;
                            soluongQuyDoi = 0;
                            soluongChuyen = StaticUtil.GetDouble(spinSoLuongChuyen.Value, 0);
                            soluongQuyDoi = soluongChuyen * StaticUtil.GetDouble(quyDoiBO.HeSoQuyDoi.Value, 0);
                            spinSoLuongQuyDoi.Value = (decimal)soluongQuyDoi;
                            soluongD = 0;
                            spinSoLuongN.Value = (decimal)soluongN - (decimal)soluongChuyen;
                            spinSoLuongD.Value = (decimal)soluongD + (decimal)soluongQuyDoi;
                        }
                        else
                        {
                            soluongChuyen = StaticUtil.GetDouble(spinSoLuongChuyen.Value, 0);
                            soluongQuyDoi = soluongChuyen * StaticUtil.GetDouble(quyDoiBO.HeSoQuyDoi.Value, 0);
                            soluongD = StaticUtil.GetDouble(MaVachBOD.SoLuongTon.Value, 0);
                            spinSoLuongQuyDoi.Value = (decimal)soluongQuyDoi;
                            spinSoLuongN.Value = (decimal)soluongN - (decimal)soluongChuyen;
                            spinSoLuongD.Value = (decimal)soluongD + (decimal)soluongQuyDoi;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Số lượng chuyển quá số lượng còn trong Kho Nguồn! ", "Cảnh báo! ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    spinSoLuongChuyen.Value = 0;
                    spinSoLuongChuyen.Focus();
                }
            }
        }

        private void spinSoLuongChuyen_EditValueChanged(object sender, EventArgs e)
        {
            lookUpKhoD_EditValueChanged(e, e);
        }
    }
}